package com.party.admin.web.controller.system;

import com.google.common.base.Strings;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.system.SysConfig;
import com.party.core.model.system.SysConfigType;
import com.party.core.service.system.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * Created by wei.li
 *
 * @date 2017/8/4 0004
 * @time 15:39
 */
@Controller
@RequestMapping(value = "/system/config")
public class ConfigController {

    @Autowired
    private ISysConfigService sysConfigService;


    /**
     * 系统配置列表
     * @param sysConfig 系统配置
     * @param page 分页参数
     * @return 交互数据
     */
    @RequestMapping(value = "list")
    public ModelAndView list(SysConfig sysConfig, Page page){
        page.setLimit(20);
        ModelAndView modelAndView = new ModelAndView("/system/config/list");
        List<SysConfig> list = sysConfigService.listPage(sysConfig, page);
        Map<Integer, String> types = SysConfigType.convertMap();
        modelAndView.addObject("list", list);
        modelAndView.addObject("types", types);
        modelAndView.addObject("page", page);
        return modelAndView;
    }

    /**
     * 系统配置视图
     * @param id 编号
     * @return 交互数据
     */
    @RequestMapping(value = "view")
    public ModelAndView view(String id){
        ModelAndView modelAndView = new ModelAndView("/system/config/view");
        SysConfig sysConfig;
        if (Strings.isNullOrEmpty(id)){
            sysConfig = new SysConfig();
        }
        else {
            sysConfig = sysConfigService.get(id);
        }
        Map<Integer, String> types = SysConfigType.convertMap();
        modelAndView.addObject("types", types);
        modelAndView.addObject("sysConfig", sysConfig);
        return modelAndView;
    }

    /**
     * 系统参数保存
     * @param sysConfig 系统参数
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxResult save(SysConfig sysConfig){
        AjaxResult ajaxResult = new AjaxResult();
        if (Strings.isNullOrEmpty(sysConfig.getId())){
            sysConfigService.insert(sysConfig);
        }
        else {
            sysConfigService.update(sysConfig);
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 删除系统参数
     * @param id 编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    public AjaxResult delete(String id){
        AjaxResult ajaxResult = new AjaxResult();
        sysConfigService.delete(id);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * 检查配置的code唯一性
     *
     * @param configId 配置id
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping("checkCodeUnique")
    public Boolean checkCodeUnique(String configId, String code) {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setCode(code);
        List<SysConfig> sysConfigs = sysConfigService.list(sysConfig);
        if (StringUtils.isEmpty(configId)) {
            if (sysConfigs.size() > 0) {
                return false;
            }
        } else {
            for (SysConfig config : sysConfigs) {
                if (!config.getId().equals(configId)) {
                    return false;
                }
            }
        }
        return true;
    }
}
